package com.workspace.controller;

import com.workspace.daomain.Customers;
import com.workspace.daomain.Photo;
import com.workspace.repository.CustomerRepository;
import com.workspace.repository.PhotoRepository;
import com.workspace.service.CustomerService;
import com.workspace.service.PhotoSevice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class AdminController {
    @Autowired
    private CustomerRepository customerRepository;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private PhotoSevice photoSevice;
    @Autowired
    private PhotoRepository photoRepository;

    //后台管理
    @RequestMapping("/admin")
    public String admin(@RequestParam("username") String username,
                        @RequestParam("password") String password,
                        Model model, HttpSession session) {
        Customers customers = customerService.findCustomerByUsername(username);
        if (!password.trim().equals(customers.getPassword()) || !customers.getRole().equals("admin")) {
            model.addAttribute("reminder", "请确认是否输入信息，并且检查用户名和密码");
            return "/admin/adminlogin";
        } else
            session.setAttribute("admin", customers.getRole());
        return "/admin/admin";


    }

    //用户信息
    @RequestMapping("/customer")
    public String Customer(Model model) {
        List<Customers> customersList = customerService.findAllCustomers();
        model.addAttribute("customersList", customersList);
        return "/admin/customers";
    }

    //    删除用户
    @RequestMapping("/userdelete")
    public void userDell(@RequestParam("sid") String sid, Model model, HttpServletResponse response) {
        int status = customerRepository.deleteCustomersBySid(sid);
        model.addAttribute("status", "删除成功");
        try {
            response.sendRedirect("/customer");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    //查看图片数据
    @RequestMapping("/photoList")
    public String findAllPhotos(Model model) {
        List<Photo> photoList = photoSevice.findAllPhotos();
        model.addAttribute("photoList", photoList);
        return "/admin/photos";

    }

    //图片数据添加跳转
    @GetMapping("/photopush")
    public String photoPush() {
        return "/admin/photopush";
    }

    //图库数据添加
    @RequestMapping("/photoadd")
    public void photoAdd(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String url = request.getParameter("url");
        String author = request.getParameter("author");
        Integer cid = Integer.valueOf(request.getParameter("cid"));
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
        String d = format.format(date);
        Photo photo = new Photo();
        photo.setUrl(url);
        photo.setAuthor(author);
        photo.setCategory(cid);
        photo.setPdate(d);
        photoRepository.save(photo);
        response.sendRedirect("/photoList");

    }
    //图库数据删除
    @RequestMapping("/photodel")
    public void photoDelete(@RequestParam("id") Integer id,HttpServletResponse response) throws IOException {
        photoSevice.deletePhoto(id);
        response.sendRedirect("/photoList");

    }

    //返回admin
    @GetMapping("/adminindex")
    public String adminIndex() {
        return "/admin/admin";
    }
    //退出网站
    @GetMapping("/clear")
    public void clearAll(HttpSession session,HttpServletResponse response) throws IOException, InterruptedException {
session.removeAttribute("admin");
        session.removeAttribute("user");
        response.sendRedirect("/loginout");
    }
}